# 接球游戏
import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))

# 设置标题
pygame.display.set_caption("Catch the Ball Game")

# 设置颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 设置玩家平台属性
player_width = 100
player_height = 20
player_x = (screen_width - player_width) // 2
player_y = screen_height - player_height - 10
player_speed = 10

# 设置球属性
ball_radius = 15
ball_x = random.randint(ball_radius, screen_width - ball_radius)
ball_y = 0
ball_speed = 3

# 设置游戏循环的标志
running = True

# 设置游戏循环的帧率
clock = pygame.time.Clock()

# 游戏主循环
while running:
    # 填充背景色
    screen.fill(BLACK)

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 玩家控制
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player_x > 0:
        player_x -= player_speed
    if keys[pygame.K_RIGHT] and player_x < screen_width - player_width:
        player_x += player_speed

    # 球下落
    ball_y += ball_speed
    if ball_y > screen_height:
        ball_y = 0
        ball_x = random.randint(ball_radius, screen_width - ball_radius)

    # 检测球和平台的碰撞
    if player_y < ball_y + ball_radius < player_y + player_height and \
       player_x < ball_x + ball_radius < player_x + player_width:
        ball_y = 0
        ball_x = random.randint(ball_radius, screen_width - ball_radius)

    # 绘制玩家平台
    pygame.draw.rect(screen, WHITE, (player_x, player_y, player_width, player_height))

    # 绘制球
    pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)

    # 更新屏幕
    pygame.display.flip()

    # 控制游戏帧率
    clock.tick(60)

# 退出 Pygame
pygame.quit()